<?php
/*
 * @Date: 2025-03-31 21:38:47
 * @LastEditors: 贾二小 erxiao.jia@outlook.com
 * @LastEditTime: 2025-04-16 01:09:47
 * @FilePath: /admin/app/Models/User.php
 */

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    /** @use HasFactory<\Database\Factories\UserFactory> */
    use HasApiTokens, HasFactory, Notifiable, HasRoles;


    protected $guard_name = 'sanctum';

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'mobile',
        'email',
        'name',
        'real_name',
        'sex',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var list<string>
     */
    protected $hidden = [
        'password',
        'remember_token',
        'unionid',
        'openid'
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
        ];
    }

    public function scopeKey(Builder $query, ?string $value): void
    {
        if ($value) {
            $query->where(
                fn(Builder $query) => $query->orWhere('name', 'like', '%' . $value . '%')
                    ->orWhere('real_name', 'like', '%' . $value . '%')
            );
        }
    }

    public function scopeSex(Builder $query, ?string $value): void
    {
        if ($value && $value != '[]') {
            $query->whereIn('sex', json_decode($value, true));
        }
    }
}
